package com.jinbo.footmessageworker.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

import com.jinbo.footmessageworker.R;

/**
 * 有×的输入框
 * 使用户自己设置drawableright 属性无效
 */
public class CrossEditText extends EditText implements OnFocusChangeListener {
	private Drawable dRight;
	//private Drawable dLeft;
	//private Rect rBounds;
	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
								  int arg3) {
			// TODO Auto-generated method stub

		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
									  int arg3) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable arg0) {
			handleClearButton();

		}
	};

	public CrossEditText(Context context) {
		super(context);
		init();
	}

	public CrossEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public CrossEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
	/*	if(this.getCompoundDrawables()[0]==null)
			Log.d("jinbo","testssssssss");
		else
			Log.d("jinbo","testtttttttt");*/
		this.setCompoundDrawables(this.getCompoundDrawables()[0],
				this.getCompoundDrawables()[1], null,
				this.getCompoundDrawables()[3]);
		this.addTextChangedListener(watcher);
		this.setOnFocusChangeListener(this);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP && dRight != null) {
			Rect rBounds = this.dRight.getBounds();
			final int x = (int) event.getX();
			final int y = (int) event.getY();
			if (x >= (this.getWidth() - rBounds.width())
					&& x <= (this.getWidth() - this.getPaddingRight())
					&& y >= this.getPaddingTop()
					&& y <= (this.getHeight() - this.getPaddingBottom())) {
				this.setText("");
				handleClearButton();
				event.setAction(MotionEvent.ACTION_CANCEL);
			}
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void setCompoundDrawables(Drawable left, Drawable top,
									 Drawable right, Drawable bottom) {
		if (right != null) {
			int width = (int) TypedValue.applyDimension(
					TypedValue.COMPLEX_UNIT_DIP, 40, getResources()
							.getDisplayMetrics());

			right.setBounds(0, 0, width, width);
			this.dRight = right;

		}	//在括号里面，使用户自己设置drawableright 属性无效
	/*	if(left!=null){
			dLeft=left;
		}*/
		super.setCompoundDrawables(left, top, right, bottom);
	}

	@Override
	protected void finalize() throws Throwable {
		this.dRight = null;
		//this.rBounds = null;
		super.finalize();
	}

	public void handleClearButton() {
		if (this.getText().toString().trim().length() > 0) {
			this.setCompoundDrawables(this.getCompoundDrawables()[0],
					this.getCompoundDrawables()[1],  this.dRight,
					this.getCompoundDrawables()[3]);

		} else {
		/*	if(dLeft!=null){
				this.setCompoundDrawables(this.dLeft,
						this.getCompoundDrawables()[1],null,
						this.getCompoundDrawables()[3]);
			}else{*/
			this.setCompoundDrawables(this.getCompoundDrawables()[0],
					this.getCompoundDrawables()[1],null,
					this.getCompoundDrawables()[3]);
			//}
		}
	}
	
	//--增加以下功能 -------zhonghw----
	/** 
     * 当CrossEditText焦点发生变化的时候，判断里面字符串长度设置清除图标的显示与隐藏 
     */  
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {   
            setClearIconVisible(getText().length() > 0);   
        } else {   
            setClearIconVisible(false);   
        }   
    }   
   
   
    /** 
     * 设置清除图标的显示与隐藏，调用setCompoundDrawables为EditText绘制上去 
     * @param visible 
     */  
    public void setClearIconVisible(boolean visible) {   
        Drawable right = visible ? getResources().getDrawable(R.drawable.selector_delete_word_button) : null;
        setCompoundDrawables(this.getCompoundDrawables()[0],null, right, null);
    }   

}
